package com.wulin;

import javax.swing.*;
import javax.swing.border.EmptyBorder;

import java.awt.*;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class Server extends JFrame {
    private JPanel contentPane;
    private JTextField textField;
    private JTextArea textArea;
    private Socket accept;
    private DataOutputStream dos;
    private DataInputStream dis;

    
    public Server() throws IOException {
        setTitle("服务器端");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 826, 710);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        textField = new JTextField();
        textField.setBounds(117, 111, 377, 35);
        contentPane.add(textField);
        textField.setColumns(10);

        JButton btnNewButton = new JButton("发送");
        btnNewButton.addActionListener(e -> {
            String mes = textField.getText();
            try {
                if (dos != null) {
                    dos.writeUTF(mes);
                    dos.flush();
                    textArea.append("发送: " + mes + "\n");
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        });
        btnNewButton.setBounds(543, 110, 153, 37);
        contentPane.add(btnNewButton);

        textArea = new JTextArea();
        textArea.setBounds(117, 243, 377, 329);
        contentPane.add(textArea);

        // 启动服务器并处理客户端连接
        new Thread(() -> {
            try {
                ServerSocket server1 = new ServerSocket(8860);
                accept = server1.accept();
                System.out.println("接收数据成功");
                dos = new DataOutputStream(accept.getOutputStream());
                dos.flush();
                dis = new DataInputStream(accept.getInputStream());
                // 处理登录验证
//                String s = dis.readUTF();
//                String[] dataArray = s.split("&");
//                String uname = "linda";
//                String upwd = "123456";
//                for (String data : dataArray) {
//                    String[] userInfo = data.split("=");
//                    if (userInfo[0].equals("uname")) {
//                        uname = userInfo[1];
//                    }
//                    if (userInfo[0].equals("upwd")) {
//                        upwd = userInfo[1];
//                    }
//                }
//                if (uname.equals("linda") && upwd.equals("123456")) {
//                    dos.writeUTF("登录成功");
//                } else {
//                    dos.writeUTF("登录失败");
//                }
                dos.flush();

                // 启动接收消息线程
                new Thread(() -> {
                    try {
                        while (true) {
                            String receivedMes = dis.readUTF();
                            textArea.append("接收: " + receivedMes + "\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }).start();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }).start();
    }
    public static void main(String[] args) {
        EventQueue.invokeLater(() -> {
            try {
                Server frame = new Server();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
